package kiwi.assist.dev.guieditor.view {
	import kiwi.assist.dev.guieditor.model.EnumConfig;
	import kiwi.assist.dev.guieditor.model.PropertyConfig;
	import kiwi.assist.dev.guieditor.model.format.BooleanFormat;
	import kiwi.assist.dev.guieditor.model.format.NumericFormat;
	import kiwi.assist.dev.guieditor.model.format.StringFormat;

	import org.aswing.AbstractCellEditor;
	import org.aswing.AsWingUtils;
	import org.aswing.Component;
	import org.aswing.Container;
	import org.aswing.JComboBox;
	import org.aswing.JLabel;
	import org.aswing.JTextField;
	import org.aswing.event.AWEvent;
	import org.aswing.event.FocusKeyEvent;
	import org.aswing.geom.IntPoint;
	import org.aswing.geom.IntRectangle;

	import flash.events.Event;
	import flash.ui.Keyboard;

	/**
	 * 根据属性的类别自动变幻于文本数字以及下拉列表甚至弹框
	 * @author zhangming.luo
	 */
	public class SmartPropertCellEditor extends AbstractCellEditor {
		private var config : PropertyConfig;
		private var textField : JTextField;
		private var comboBox : JComboBox;
		private var label : JLabel;

		public function SmartPropertCellEditor() {
			super();
			setClickCountToStart(1);
		}

		public function getTextField() : JTextField {
			if (textField == null) {
				textField = new JTextField();
			}
			return textField;
		}

		public function getComboBox() : JComboBox {
			if (comboBox == null) {
				comboBox = new JComboBox();
			}
			return comboBox;
		}

		override public function getEditorComponent() : Component {
			// 自动选择
			if (config) {
				if (config.readOnly) {
					return getLabel();
				} else if (config.enumList.length > 0) {
					return getComboBox();
				} else {
					if (config.format is NumericFormat) {
						return getTextField();
					} else if (config.format is StringFormat ) {
						return getTextField();
					} else if (config.format is BooleanFormat) {
						return getComboBox();
					}
				}
			}
			return getTextField();
		}

		private function getLabel() : Component {
			if (label == null) {
				label = new JLabel();
			}
			return label;
		}

		override public function getCellEditorValue() : * {
			if (config) {
				if (config.enumList.length > 0) {
					config.value = getComboBox().getSelectedItem();
				} else {
					if (config.format is NumericFormat) {
						config.value = getTextField().getText();
					} else if (config.format is StringFormat) {
						config.value = getTextField().getText();
					} else if (config.format is BooleanFormat) {
						config.value = getComboBox().getSelectedItem();
					}
				}
			}
			return config;
		}

		override public function startCellEditing(owner : Container, value : *, bounds : IntRectangle) : void {
			popup.changeOwner(AsWingUtils.getOwnerAncestor(owner));
			var gp : IntPoint = owner.getGlobalLocation().move(bounds.x, bounds.y);
			popup.setSizeWH(bounds.width, bounds.height);
			popup.show();
			popup.setGlobalLocation(gp);
			popup.validate();
			popup.toFront();
			popup.removeAll();

			setCellEditorValue(value);
			var com : Component = getEditorComponent();
			com.removeEventListener(AWEvent.ACT, __editorComponentAct);
			com.removeEventListener(AWEvent.FOCUS_LOST, __editorComponentFocusLost);
			com.removeEventListener(FocusKeyEvent.FOCUS_KEY_DOWN, __editorComponentKeyDown);
			com.setSizeWH(bounds.width, bounds.height);
			popup.append(com);
			com.requestFocus();
			if (com is Container && !com.isFocusOwner()) {
				var con : Container = Container(com);
				var sub : Component;
				sub = con.getFocusTraversalPolicy().getDefaultComponent(con);
				if (sub != null) sub.requestFocus();
				if (sub == null || !sub.isFocusOwner()) {
					for (var i : int = 0; i < con.getComponentCount(); i++) {
						sub = con.getComponent(i);
						sub.requestFocus();
						if (sub.isFocusOwner()) {
							break;
						}
					}
				}
			}
			com.addEventListener(AWEvent.ACT, __editorComponentAct);
			com.addEventListener(AWEvent.FOCUS_LOST, __editorComponentFocusLost);
			com.addEventListener(FocusKeyEvent.FOCUS_KEY_DOWN, __editorComponentKeyDown);
			com.validate();
		}

		private function __editorComponentFocusLost(e : Event) : void {
			cancelCellEditing();
		}

		private function __editorComponentAct(e : Event) : void {
			stopCellEditing();
		}

		private function __editorComponentKeyDown(e : FocusKeyEvent) : void {
			if (e.keyCode == Keyboard.ESCAPE) {
				cancelCellEditing();
			}
		}

		override protected function setCellEditorValue(value : *) : void {
			if (value is PropertyConfig) {
				this.config = value;
				// 自动选择
				if (config.enumList.length > 0) {
					getComboBox().setListData(config.enumList);
				} else {
					if (config.format is NumericFormat) {
						getTextField().setText(value + "");
						getTextField().selectAll();
					} else if (config.format is StringFormat) {
						getTextField().setText(value + "");
						getTextField().selectAll();
					} else if (config.format is BooleanFormat) {
						getComboBox().setListData([EnumConfig.TRUE, EnumConfig.FALSE]);
					}
				}
			} else {
				// 全部当做是文本处理
				getTextField().setText(value + "");
				getTextField().selectAll();
			}
		}
	}
}
